home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Polyhedra / Polyhedra.java < prev    next >
Encoding:
Java Source  |  2001-06-23  |  13.1 KB  |  463 lines

  1. /*
  2.     Polyhedra by Scott Ziegler
  3.     06.22.01
  4.     For use with Simulcra RPG and AD&D.
  5.     
  6.     This file and its intellectual contents are considered property of the creator and are to be     treated as such with respect to use in other applications.  Any use of this software for any     purpose whatsoever must have explicit permission from the author of the file.  This code is     part of an ongoing development process for future possible products, and so is considered     private property.
  7.     Do not use without permission.  Author: Scott C. Ziegler <Aslan@Narnia.net>
  8.     
  9.     Instructions for use:
  10.     
  11.     Code in use by:
  12.     
  13.     version history:
  14.     
  15.     10.04.00 - Began the project.
  16.     10.05.00 - reached a syntactically correct version.
  17.     06.22.01 - refitted project with Project Builder (MacOS X) and renamed to Polyhedra from             DiceRoller at MacHack 2001 (16th/5th)
  18.     
  19.     Work Needed:
  20.     
  21.     Work Completed:
  22.     
  23. */
  24.  
  25. //package Polyhedra;
  26.  
  27. import java.awt.*;
  28. import java.awt.event.*;
  29. import java.awt.Window;
  30. import javax.swing.*;
  31. import javax.swing.border.*;
  32. import javax.swing.event.*;
  33.  
  34.  
  35. public class Polyhedra extends JFrame {
  36.  
  37.     public static final Font     DEFAULT_SYS_FONT = new Font("Courier", Font.PLAIN, 10);
  38.     public static final Font    DEFAULT_ROLL_FONT = new Font("Courier", Font.PLAIN, 36);
  39.     public static final String    DEFAULT_SYS_PROMPT = new String(">");
  40.     public static final Arcana.ADDDice    DEFAULT_DIE = new Arcana.ADDDice();
  41.  
  42.     JMenuBar            menuBar;
  43.     JMenu                fileMenu;
  44.         JMenuItem            aboutBoxMenuItem;
  45.         JMenuItem            quitMenuItem;
  46.     JMenu                editMenu;
  47.         JMenuItem            undoMenuItem;
  48.         JMenuItem            cutMenuItem;
  49.         JMenuItem            copyMenuItem;
  50.         JMenuItem            pasteMenuItem;
  51.     JMenu                optionsMenu;
  52.         JMenuItem             optionsMenuItem;
  53.     JMenu                rollMenu;
  54.         JMenuItem            rollMenuItem;
  55.         
  56.     JButton                rollBtn;
  57.     JButton                twoSidedBtn;
  58.     JButton                threeSidedBtn;
  59.     JButton                fourSidedBtn;
  60.     JButton                sixSidedBtn;
  61.     JButton                eightSidedBtn;
  62.     JButton                tenSidedBtn;
  63.     JButton                twelveSidedBtn;
  64.     JButton                twentySidedBtn;
  65.     JButton                thirtySidedBtn;
  66.     JButton                hundredSidedBtn;
  67.     
  68.     JCheckBox            timesCheckBox;
  69.     
  70.     JLabel                rollLabel;
  71.     
  72.     JTextArea            displayTextArea;
  73.     JTextField            dieTextField;
  74.     JTextField            timesTextField;
  75.     
  76.     JScrollPane            displayScrollPane;
  77.     JScrollBar            displayScrollBar;
  78.     BoundedRangeModel    scrollModel;
  79.     int                    oldMax;
  80.     
  81.     JPanel                devicePanel;
  82.     JPanel                buttonPanel;
  83.     JPanel                checkPanel;
  84.         
  85.     Font                systemFont;
  86.     Font                rollFont;
  87.     
  88.     String                systemPrompt;
  89.     boolean                systemPromptSwitch;
  90.     
  91.     ActionListener        diceButtonListener;
  92.     
  93.     Arcana.ADDDice                mainDie;
  94.     
  95.     public Polyhedra() {
  96.         
  97.         super("Polyhedra");
  98.         addWindowListener(new BasicWindowMonitor());
  99.         
  100.         Insets objectInsets = new Insets(2,2,2,2);
  101.         
  102.         mainDie = DEFAULT_DIE;
  103.         
  104.         setSystemPromptSwitch(false);
  105.         setSystemPrompt(DEFAULT_SYS_PROMPT);
  106.         
  107.         systemFont = DEFAULT_SYS_FONT;
  108.         rollFont = DEFAULT_ROLL_FONT;
  109.         
  110.         // Menus and MenuBar and MenuItems
  111.         menuBar = new JMenuBar();
  112.         menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
  113.         
  114.         fileMenu = new JMenu("File");
  115.         fileMenu.setMnemonic('F');
  116.         aboutBoxMenuItem = new JMenuItem("About…");
  117.         fileMenu.add(aboutBoxMenuItem);
  118.         aboutBoxMenuItem.addActionListener(new ActionListener() {
  119.             public void actionPerformed(ActionEvent ae) {
  120.                 /*printEndLine("\nThoth Interaction System \n" +
  121.                              "Written by Scott Ziegler \n" +
  122.                              "¢ Simulcra 1999");
  123.                              */
  124.                 }
  125.             });
  126.         quitMenuItem = new JMenuItem("Quit");
  127.         fileMenu.add(quitMenuItem);
  128.         quitMenuItem.addActionListener(new ActionListener() {
  129.             public void actionPerformed(ActionEvent ae) {
  130.                 System.exit(0);
  131.                 }
  132.             });
  133.         editMenu = new JMenu("Edit");
  134.         undoMenuItem = new JMenuItem("Undo");
  135.         editMenu.add(undoMenuItem);
  136.         cutMenuItem = new JMenuItem("Cut");
  137.         editMenu.add(cutMenuItem);
  138.         copyMenuItem = new JMenuItem("Copy");
  139.         editMenu.add(copyMenuItem);
  140.         pasteMenuItem = new JMenuItem("Paste");
  141.         editMenu.add(pasteMenuItem);
  142.         optionsMenu = new JMenu("Options");
  143.         optionsMenuItem = new JMenuItem("Options…");
  144.         optionsMenu.add(optionsMenuItem);
  145.         rollMenu = new JMenu("Roll");
  146.         rollMenuItem = new JMenuItem("Roll…");
  147.         menuBar.add(fileMenu);
  148.         menuBar.add(editMenu);
  149.         menuBar.add(optionsMenu);
  150.         menuBar.add(rollMenu);
  151.         
  152.         // Roll Button
  153.         rollBtn = new JButton("Roll");
  154.         rollBtn.addActionListener(new ActionListener() {
  155.             public void actionPerformed(ActionEvent ae) {
  156.                 // setRollLabel(/*call to aux. which returns String of roll*/);
  157.                 if (dieTextField.getText() != "") {
  158.                     mainDie = mainDie.stringToDie(dieTextField.getText());
  159.                     if (!timesCheckBox.getModel().isSelected()) {
  160.                         int roll = mainDie.roll();
  161.                         setRollLabel("" + roll);
  162.                         displayPrompt();
  163.                         displayPrintLine("" + roll);
  164.                         }
  165.                     else {
  166.                         try {
  167.                             int numberOfRolls = Integer.parseInt(timesTextField.getText());
  168.                             displayPrint("" + numberOfRolls +"x,");
  169.                             displayPrompt();
  170.                             int rollHolder;
  171.                             for (int i=0; i < numberOfRolls; i++) {
  172.                                 rollHolder = mainDie.roll();
  173.                                 displayPrint("" + rollHolder + ",");
  174.                                 }
  175.                             displayPrint(".");
  176.                             displayEndline();
  177.                             }
  178.                         catch (NumberFormatException nfe) {
  179.                             
  180.                             }
  181.                         }
  182.                     }
  183.                 }
  184.             });
  185.             
  186.         // Dice Buttons (all in one handler)
  187.         diceButtonListener = new ActionListener() {
  188.             public void actionPerformed(ActionEvent ae) {
  189.                 String com = ae.getActionCommand();
  190.                      if (com.compareTo("1d2") == 0) {
  191.                          mainDie.setCoefficient(1);
  192.                          mainDie.setDie(2);
  193.                          mainDie.setModifier(0);
  194.                         setRollLabel("" + mainDie.roll());
  195.                         }
  196.                 else if (com.compareTo("1d3") == 0) {
  197.                     mainDie.setCoefficient(1);
  198.                     mainDie.setDie(3);
  199.                     mainDie.setModifier(0);
  200.                     setRollLabel("" + mainDie.roll());
  201.                     }
  202.                 else if (com.compareTo("1d4") == 0) {
  203.                     mainDie.setCoefficient(1);
  204.                     mainDie.setDie(4);
  205.                     mainDie.setModifier(0);
  206.                     setRollLabel("" + mainDie.roll());
  207.                     }
  208.                 else if (com.compareTo("1d6") == 0) {
  209.                     mainDie.setCoefficient(1);
  210.                     mainDie.setDie(6);
  211.                     mainDie.setModifier(0);
  212.                     setRollLabel("" + mainDie.roll());
  213.                     }
  214.                 else if (com.compareTo("1d8") == 0) {
  215.                     mainDie.setCoefficient(1);
  216.                     mainDie.setDie(8);
  217.                     mainDie.setModifier(0);
  218.                     setRollLabel("" + mainDie.roll());
  219.                     }
  220.                 else if (com.compareTo("1d10") == 0) {
  221.                     mainDie.setCoefficient(1);
  222.                     mainDie.setDie(10);
  223.                     mainDie.setModifier(0);
  224.                     setRollLabel("" + mainDie.roll());
  225.                     }
  226.                 else if (com.compareTo("1d12") == 0) {
  227.                     mainDie.setCoefficient(1);
  228.                     mainDie.setDie(12);
  229.                     mainDie.setModifier(0);
  230.                     setRollLabel("" + mainDie.roll());
  231.                     }
  232.                 else if (com.compareTo("1d20") == 0) {
  233.                     mainDie.setCoefficient(1);
  234.                     mainDie.setDie(20);
  235.                     mainDie.setModifier(0);
  236.                     setRollLabel("" + mainDie.roll());
  237.                     }
  238.                 else if (com.compareTo("1d30") == 0) {
  239.                     mainDie.setCoefficient(1);
  240.                     mainDie.setDie(30);
  241.                     mainDie.setModifier(0);
  242.                     setRollLabel("" + mainDie.roll());
  243.                     }
  244.                 else if (com.compareTo("1d100") == 0) {
  245.                     mainDie.setCoefficient(1);
  246.                     mainDie.setDie(100);
  247.                     mainDie.setModifier(0);
  248.                     setRollLabel("" + mainDie.roll());
  249.                     }
  250.                 }
  251.             };
  252.         // Dice Button Initialization
  253.         twoSidedBtn = new JButton("1d2");
  254.         twoSidedBtn.addActionListener(diceButtonListener);
  255.         threeSidedBtn = new JButton("1d3");
  256.         threeSidedBtn.addActionListener(diceButtonListener);
  257.         fourSidedBtn = new JButton("1d4");
  258.         fourSidedBtn.addActionListener(diceButtonListener);
  259.         sixSidedBtn = new JButton("1d6");
  260.         sixSidedBtn.addActionListener(diceButtonListener);
  261.         eightSidedBtn = new JButton("1d8");
  262.         eightSidedBtn.addActionListener(diceButtonListener);
  263.         tenSidedBtn = new JButton("1d10");
  264.         tenSidedBtn.addActionListener(diceButtonListener);
  265.         twelveSidedBtn = new JButton("1d12");
  266.         twelveSidedBtn.addActionListener(diceButtonListener);
  267.         twentySidedBtn = new JButton("1d20");
  268.         twentySidedBtn.addActionListener(diceButtonListener);
  269.         thirtySidedBtn = new JButton("1d30");
  270.         thirtySidedBtn.addActionListener(diceButtonListener);
  271.         hundredSidedBtn = new JButton("1d100");
  272.         hundredSidedBtn.addActionListener(diceButtonListener);
  273.         
  274.         
  275.         // JLabel decl and event handler for redraw
  276.         rollLabel = new JLabel("0000");
  277.         rollLabel.setFont(rollFont);
  278.         rollLabel.setHorizontalAlignment(SwingConstants.CENTER);
  279.         rollLabel.setVerticalAlignment(SwingConstants.CENTER);
  280.         
  281.         // JTextArea
  282.         displayTextArea = new JTextArea(/*20,30*/); // this size doesn't work.
  283.         displayTextArea.setEditable(false);
  284.         displayTextArea.setLineWrap(true);
  285.         displayTextArea.setWrapStyleWord(true);
  286.         displayTextArea.setFont(systemFont);
  287.         // JScrollPane and JScrollBar "advancer"
  288.         displayScrollPane = new JScrollPane(displayTextArea);
  289.         displayScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  290.         displayScrollBar = displayScrollPane.getVerticalScrollBar();
  291.         scrollModel = displayScrollBar.getModel();
  292.         oldMax = 0;
  293.         scrollModel.addChangeListener(new ChangeListener() {
  294.             public void stateChanged(ChangeEvent ce) {
  295.                 BoundedRangeModel sourceModel = (BoundedRangeModel)ce.getSource();
  296.                 int newMax = sourceModel.getMaximum();
  297.                 if (newMax > oldMax) {
  298.                     scrollModel.setValue(newMax);
  299.                     oldMax = newMax;
  300.                     }
  301.                 }
  302.             });
  303.         
  304.         // JTextFields
  305.         dieTextField = new JTextField();
  306.         dieTextField.setFont(systemFont);
  307.         timesTextField = new JTextField();
  308.         timesTextField.setFont(systemFont);
  309.         
  310.         // JCheckBox decl and event handler (grey-out when unchecked)
  311.         timesCheckBox = new JCheckBox("Times:", false);
  312.         timesCheckBox.addItemListener(new ItemListener() {
  313.             public void itemStateChanged(ItemEvent ie) {
  314.                 if (ie.getStateChange() == ItemEvent.SELECTED) {
  315.                     timesTextField.setEnabled(true);
  316.                     }
  317.                 if (ie.getStateChange() == ItemEvent.DESELECTED) {
  318.                     timesTextField.setEnabled(false);
  319.                     }
  320.                 }
  321.             });
  322.             
  323.         // Set up Layout
  324.         
  325.         TitledBorder dieTitle = new TitledBorder("Die");
  326.         dieTitle.setTitleJustification(TitledBorder.LEFT);
  327.         dieTitle.setTitlePosition(TitledBorder.TOP);
  328.         dieTextField.setBorder(dieTitle);
  329.         
  330.         TitledBorder rollTitle = new TitledBorder("Roll");
  331.         rollTitle.setTitleJustification(TitledBorder.LEFT);
  332.         rollTitle.setTitlePosition(TitledBorder.TOP);
  333.         rollLabel.setBorder(rollTitle);
  334.         
  335.         
  336.         checkPanel = new JPanel(new GridLayout(2,1));
  337.         checkPanel.add(timesCheckBox);
  338.         checkPanel.add(timesTextField);
  339.         
  340.         devicePanel = new JPanel();
  341.         devicePanel.setLayout(new GridBagLayout());
  342.         GridBagConstraints c = new GridBagConstraints();
  343.         
  344.         c.gridx = 0; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1;
  345.         c.weightx = 80; c.weighty = 25;
  346.         c.fill = GridBagConstraints.BOTH;
  347.         c.anchor = GridBagConstraints.CENTER;
  348.         c.insets = objectInsets;
  349.         devicePanel.add(dieTextField, c);
  350.         
  351.         c.gridx = 1; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1;
  352.         c.weightx = 20; c.weighty = 25;
  353.         c.fill = GridBagConstraints.NONE;
  354.         c.anchor = GridBagConstraints.CENTER;
  355.         c.insets = objectInsets;
  356.         devicePanel.add(rollBtn, c);
  357.         
  358.         c.gridx = 0; c.gridy = 1; c.gridheight = 1; c.gridwidth = 1;
  359.         c.weightx = 80; c.weighty = 75;
  360.         c.fill = GridBagConstraints.BOTH;
  361.         c.anchor = GridBagConstraints.CENTER;
  362.         c.insets = objectInsets;
  363.         devicePanel.add(rollLabel, c);
  364.         
  365.         c.gridx = 1; c.gridy = 1; c.gridheight = 1; c.gridwidth = 1;
  366.         c.weightx = 20; c.weighty = 75;
  367.         c.fill = GridBagConstraints.NONE;
  368.         c.anchor = GridBagConstraints.NORTH;
  369.         c.insets = objectInsets;
  370.         devicePanel.add(checkPanel, c);
  371.         
  372.         buttonPanel = new JPanel();
  373.         buttonPanel.setLayout(new GridLayout(2,5,5,5)); // 2 rows, 5 cols, 5 hpix, 5 vpix
  374.         // should I set insets?
  375.         buttonPanel.add(twoSidedBtn);
  376.         buttonPanel.add(threeSidedBtn);
  377.         buttonPanel.add(fourSidedBtn);
  378.         buttonPanel.add(sixSidedBtn);
  379.         buttonPanel.add(eightSidedBtn);
  380.         buttonPanel.add(tenSidedBtn);
  381.         buttonPanel.add(twelveSidedBtn);
  382.         buttonPanel.add(twentySidedBtn);
  383.         buttonPanel.add(thirtySidedBtn);
  384.         buttonPanel.add(hundredSidedBtn);
  385.         
  386.         // initialize window, add components, and show
  387.         setJMenuBar(menuBar);
  388.         Container progFrameContentPane = getContentPane();
  389.         progFrameContentPane.add(devicePanel, BorderLayout.NORTH);
  390.         progFrameContentPane.add(displayScrollPane, BorderLayout.CENTER);
  391.         progFrameContentPane.add(buttonPanel, BorderLayout.SOUTH);
  392.         
  393.         setSize(new Dimension(385,440));
  394.         setVisible(true);
  395.         dieTextField.requestFocus();
  396.         
  397.         
  398.         }
  399.         
  400.     // Selectors
  401.     
  402.     public String getSystemPrompt() {
  403.         return systemPrompt;
  404.         }
  405.  
  406.     public boolean getSystemPromptSwitch() {
  407.         return systemPromptSwitch;
  408.         }
  409.         
  410.     public Font getSystemFont() {
  411.         return systemFont;
  412.         }
  413.         
  414.     public Font getRollFont() {
  415.         return rollFont;
  416.         }
  417.     
  418.     // Mutators
  419.     
  420.     public void setSystemPrompt(String prompt) {
  421.         systemPrompt = prompt;
  422.         }
  423.     
  424.     public void setSystemPromptSwitch(boolean bool) {
  425.         systemPromptSwitch = bool;
  426.         }
  427.         
  428.     public void setSystemFont(Font font) {
  429.         systemFont = font;
  430.         }
  431.         
  432.     public void setRollFont(Font font) {
  433.         rollFont = font;
  434.         }
  435.         
  436.     public void setRollLabel(String newValue) {
  437.         rollLabel.setText(newValue);
  438.         rollLabel.repaint();
  439.         }
  440.         
  441.     public void displayPrompt() {
  442.         displayTextArea.append(mainDie.toString() + ":");
  443.         displayTextArea.append(getSystemPrompt());
  444.         }
  445.         
  446.     public void displayPrint(String strToDisp) {
  447.         displayTextArea.append(strToDisp);
  448.         }
  449.         
  450.     public void displayPrintLine(String strToDisp) {
  451.         displayTextArea.append(strToDisp + "\n");
  452.         }
  453.         
  454.     public void displayEndline() {
  455.         displayTextArea.append("\n");
  456.         }
  457.     
  458.     // Main entry point
  459.     static public void main(String[] args) {
  460.         new Polyhedra();
  461.         }
  462.     
  463.     }